package cn.livingCloud.service.impl.operation;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.entity.content.Button;
import cn.livingCloud.repository.ButtonMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sujiantang on 2016/1/8.
 */
public class ButtonTreeLoader {
    protected transient ButtonMapper mapper = BeanProvider.getBean(ButtonMapper.class);

    public List<Button> loadTree() {
        List<Button> list = mapper.findAllButtonList();

        return getParentBtn(list);
    }

    //获取所有一级菜单
    private List<Button> getParentBtn(List<Button> list) {
        List<Button> parentList = new ArrayList<>();
        for (Button button : list) {
            if (button.isParent()) {
                parentList.add(button);
            }
        }
        getSubBtn(list, parentList);
        return parentList;
    }

    //获取所有二级菜单加入到一级菜单
    private void getSubBtn(List<Button> list, List<Button> parentList) {
        for (Button button : list) {
            for (Button parentBtn : parentList) {
                String parentId = parentBtn.getUuid();
                if (parentId.equals(button.getParentId())) {
                    parentBtn.addSubButton(button);
                }
            }
        }
    }
}
